package com.example.yls.demoa;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    private RecyclerView rlvStu;
    private StudentAdapter adapter;
    private ArrayList<Student> studentArrayList = new ArrayList<>();
    private ArrayList<Student> searchResultList = new ArrayList<>();
    private String no = "10086";
    private EditText edtKeyWord;
    private Button btnSreach;

    private boolean isResultList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        rlvStu = findViewById(R.id.rlv_stu);
        edtKeyWord = findViewById(R.id.edt_key);

        initStudentArrayList();

        adapter = new StudentAdapter(MainActivity.this,studentArrayList);
        rlvStu.setAdapter(adapter);
        LinearLayoutManager manager = new LinearLayoutManager(MainActivity.this);
        rlvStu.setLayoutManager(manager);

        setStuAddClickListener();

        intSreachView();

    }

    //初始化搜索相关视图
    private void intSreachView() {
        findViewById(R.id.btn_sreach).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String key = edtKeyWord.getText().toString();
                sreach(studentArrayList,key);
                adapter.changeData(searchResultList);
                isResultList = true;
            }
        });

        edtKeyWord.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String key = edtKeyWord.getText().toString();
                sreach(studentArrayList,key);
                adapter.changeData(searchResultList);
                isResultList = true;
            }
        });

    }

    //搜索方法
    private void sreach(ArrayList<Student> studentArrayList, String key) {
        searchResultList.clear();
        for(int i = 0; i<studentArrayList.size(); i++){
            Student s = studentArrayList.get(i);
            if(s.getName().contains(key)){
                searchResultList.add(s);
            }else if(String.valueOf(s.getAge()).equals(key)){
                searchResultList.add(s);
            }
        }

    }

    /**
     * 添加学生点击事件
     */
    private void setStuAddClickListener() {
        findViewById(R.id.open_to_add_stu).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this,addActivity.class);
                startActivityForResult(intent,20010);
            }
        });
    }


    /**
     * 初始化学生列表
     */
    private void initStudentArrayList() {
        Student s1 = new Student("10081","张一",0,R.drawable.dog1);
        Student s2 = new Student("10082","张而",0,R.drawable.dog2);
        Student s3 = new Student("10083","张三",0,R.drawable.dog3);
        Student s4 = new Student("10084","张死",0,R.drawable.dog4);
        Student s5 = new Student("10085","张无",0,R.drawable.dog5);
        studentArrayList.add(s1);
        studentArrayList.add(s2);
        studentArrayList.add(s3);
        studentArrayList.add(s4);
        studentArrayList.add(s5);
    }

    /**
     * 处理返回的数据
     * @param requestCode
     * @param resultCode
     * @param data
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(data == null){
            return;
        }
        if(resultCode == 20011) {
            no = String.valueOf(Integer.parseInt(no)+1);
            Student s = new Student(no,data.getStringExtra("NAME"),
                    data.getIntExtra("AGE", 0),
                    data.getIntExtra("IMG", R.drawable.dog1));
            studentArrayList.add(s);
            adapter.notifyDataSetChanged();
        }
    }

    /**
     * 点击编辑学生数据事件
     */
    /**
     * 点击列表目标跳转到该目标数据的修改页面
     */
    private void setToEdtStudentClickListener() {

    }
}

